// pages/list/list.js
// import moment from 'moment'

Page({

    /**
     * 页面的初始数据
     */
    data: {
      option1: [{
          text: '全部发布',
          value: 0
        },
        {
          text: '闲置物品',
          value: 1
        },
        {
          text: '求购物品',
          value: 2
        },
        {
          text: '免费物品',
          value: 3
        }
       
      ],
      value1: 0,
      value2: 'exchange',
      goodsList: [],
      askList:[],
      searchVal: '',
      skip: 0,
      openId:'',
      freeList:[],
      totalList:[]
    },
  
    onChange: function(e) {
      console.log(e)
      if (e.currentTarget.dataset.type == "op1") {
          if(e.detail == 1){
              this.getGoodsList()
          }
          else if(e.detail == 2){
              this.getAskList()
          }
          else if(e.detail == 3){
              this.getFreeList()
          }
        this.setData({
          value1: Number(e.detail)
        })
      } else {
        this.setData({
          value2: e.detail
        })
      }
    //   this.getGoodsList('set')
    },
   
    // 跳转到详情页
    goInfo: function(e) {
      if (!e.currentTarget.dataset.id) {
        wx.showToast({
          title: '商品id不能为空',
          icon: 'none',
          duration: 2000
        })
        return
      }
      wx.navigateTo({
        url: '/pages/goods/goods?id=' + e.currentTarget.dataset.id,
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      const openId = wx.getStorageSync('openid');
      this.setData({
          openId
      })
     this.getAskList();
     this.getFreeList();
     this.getGoodsList();
    
      wx.showNavigationBarLoading()
    
      
    },
  
    //获取闲置物品列表
    getGoodsList(){
        wx.request({
            url: 'http://127.0.0.1:6060/api/getUserGoods',
            data: {
                userId:this.data.openId
            },
            header: {'content-type':'application/json'},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {
                console.log(result.data.data)
                this.setData({
                    goodsList:result.data.data
                })
            },
           
        });
          
    },

    //获取求购物品列表
    getAskList(){
        wx.request({
            url: 'http://127.0.0.1:6060/api/getUserAskList',
            data: {
                userId:this.data.openId
            },
            header: {'content-type':'application/json'},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {
                console.log(result.data.data)
                this.setData({
                    askList:result.data.data
                })
            },
           
        });      
    },


// 修改发布
    updatePublish(e){
      console.log(e.currentTarget.dataset.id)
    
      wx.navigateTo({
          url:'/pages/publishDetail/publishDetail?goodsId='+e.currentTarget.dataset.id,
      });
  },
    
    // 删除闲置发布
    deleteGoodsPublish(e){
     
      const goodsId = e.currentTarget.dataset.id
      console.log('delete')
      wx.showModal({
         
          content: '确定删除？',
          showCancel: true,
          cancelText: '取消',
          cancelColor: 'grey',
          confirmText: '确定',
          confirmColor: '#3CC51F',
          success: (result) => {
              if (result.confirm) {
                  wx.request({
                      url:'http://127.0.0.1:6060/api/deleteGoods',
                      data:{
                         goodsId
                      },
                      method:'get',
                      
                      success:(res) =>{
                          // console.log(res)
                          this.onLoad()
                          setTimeout(() => {
                            
                            wx.showToast({
                              title: '删除成功',
                              icon: 'success',
                              duration: 1500
                            })
                          }, 100);
                            
                        
                      }
                  })
              }
          },
          fail: () => {},
          complete: () => {}
      });
    },
    //获取免费物品列表
    getFreeList(){
        wx.request({
            url: 'http://127.0.0.1:6060/api/getUserFreeList',
            data: {
                userId:this.data.openId
            },
            header: {'content-type':'application/json'},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {
                console.log(result.data.data)
                this.setData({
                    freeList:result.data.data
                })
            },
           
        });      
    },
   
  
    //搜索框获取焦点
    onFocus: function() {
      if (this.prevent) {
        return
      }
      this.prevent = true
      setTimeout(() => {
        this.prevent = false
      }, 1000)
      wx.navigateTo({
        url: '../search/search',
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
      wx.hideNavigationBarLoading()
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
    //   this.getGoodsList('set')
      wx.stopPullDownRefresh()
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
    //   this.getGoodsList('add')
    }
  })